package com.wd.mylibrary_base.utils

import android.content.Context
import android.net.ConnectivityManager
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import java.util.concurrent.TimeUnit
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create

/**
 * 作者: 邢凌志
 * 时间: 2024/9/12
 * 描述: HttpUtils
 */
class HttpUtils private constructor(){
    companion object{
        val httpUtils:HttpUtils by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            HttpUtils()
        }
    }

    private val okHttpClient=OkHttpClient.Builder()
        .connectTimeout(5,TimeUnit.SECONDS)
        .readTimeout(5,TimeUnit.SECONDS)
        .addInterceptor(HttpLoggingInterceptor().apply {
            level=HttpLoggingInterceptor.Level.BODY
        })
        .build()

    private val retrofit=Retrofit.Builder()
        .client(okHttpClient)
        .baseUrl("http://10.59.9.24/")
//        .baseUrl("http://10.59.9.18/")
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build()

    private val retrofit2=Retrofit.Builder()
        .client(okHttpClient)
//        .baseUrl("http://10.59.9.24/")
        .baseUrl("http://10.59.9.18/")
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build()


    val apiService=retrofit.create<ApiService>()
    val apiService2=retrofit2.create<ApiService>()

    fun isConnect(context: Context):Boolean{
        val manager=context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val network=manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
        return network!=null && network.isConnected
    }
}